﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;


namespace eTraining
{
    public partial class TopPanel : UserControl
    {
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        public ControlBoxTool ControlButton = new ControlBoxTool();
        public ButtonBoxTool ToolButton = new ButtonBoxTool();

        private bool CloseFlg = false;

        public TopPanel()
        {
            InitializeComponent();
        }

        Bitmap bitmap = new Bitmap(1, 1);

        private int RunMode = -1;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            InitBitmap();

            Graphics g = Graphics.FromImage(bitmap);
            Draw(g);
            e.Graphics.DrawImage(bitmap, 1, 1);
            g.Dispose();
        }

        public void Draw(Graphics g)
        {
            Color c1 = ControlPaint.LightLight(BackColor);
            Color c2 = ControlPaint.Light(BackColor);
            Color c3 = BackColor;

            Rectangle rect = new Rectangle(0, 0, Math.Max(1, Width), Height / 2);
            LinearGradientBrush brush = new LinearGradientBrush(rect, c1, c2, LinearGradientMode.Vertical);
            g.FillRectangle(brush, rect);
            brush.Dispose();

            rect.Offset(0, Height / 2);
            rect.Height += 1;
            brush = new LinearGradientBrush(rect, c3, c2, LinearGradientMode.Vertical);
            g.FillRectangle(brush, rect);
            brush.Dispose();

            SolidBrush brushTitle = new SolidBrush(TitleColor);
            SizeF sf = g.MeasureString(TitleText, TitleFont);
            g.DrawString(TitleText, TitleFont, brushTitle, Width- ControlBoxTool.LeftStartX - sf.Width, (Height / 2 - sf.Height) / 2);

            brushTitle.Dispose();

            string strInfo = "张伟,你好！ 今天是" 
                + DateTime.Now.ToString("yyyy年MM月dd日") 
                + "  现在时间：" + DateTime.Now.ToString("HH时mm分ss秒");
            sf = g.MeasureString(strInfo, Font);

            g.DrawString(strInfo, Font, Brushes.Black, Width - sf.Width - 5, Height / 4 * 3 + 2 - sf.Height / 2);

            Pen BorderPen = new Pen(FindForm().BackColor, 2);
            g.DrawLine(BorderPen, 0, Height - 2, Width, Height - 2);
            BorderPen.Dispose();

            ControlButton.Draw(g, this);
            ToolButton.Draw(g, this);
        }

        private string _TitleText = "";

        public string TitleText
        {
            get { return _TitleText; }
            set { _TitleText = value; }
        }

        private Brush GetRandomBrush()
        {
            Random rand = new Random();
            SolidBrush brush = new SolidBrush(Color.FromArgb(rand.Next(int.MinValue,int.MaxValue)));

            return brush;
        }

        public Font _TitleFont = null;
        public Font TitleFont 
        {
            get
            {

                if (_TitleFont == null)
                {
                    _TitleFont = Font;
                }

                return _TitleFont;
            }
            set
            {
                _TitleFont = value;
            }
        }

        private Color _TitleColor = Color.Empty;

        public Color TitleColor
        {
            get 
            {
                if (_TitleColor == Color.Empty)
                {
                    _TitleColor = ForeColor;
                }
                return _TitleColor; 
            }
            set { _TitleColor = value; }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            int mode = ControlButton.HitTest(this);
            Form frm = FindForm();

            if (mode != -1)
            {
                if (mode == 1)
                {
                    frm.WindowState = FormWindowState.Minimized;
                }
                else if (mode == 2)
                {
                    if (frm.WindowState == FormWindowState.Maximized)
                    {
                        frm.WindowState = FormWindowState.Normal;
                    }
                    else
                    {
                        frm.WindowState = FormWindowState.Maximized;
                    }
                }
                else if (mode == 3)
                {
                    CloseFlg = true;
                }
                Refresh();
                return;
            }

            RunMode = ToolButton.HitTest(this);

            if (RunMode != -1)
            {
                Refresh();
                return;
            }

            if (e.Clicks == 1 && frm.WindowState != FormWindowState.Maximized)
            {
                ReleaseCapture();
                SendMessage(FindForm().Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            base.OnMouseDown(e);
            Refresh();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            try
            {
                MainForm frm = FindForm() as MainForm;

                if (CloseFlg == true)
                {
                    if (frm != null)
                    {
                        frm.Close();
                    }
                }

                switch (RunMode)
                {
                    case 0:
                        frm.WB.GoBack();
                        break;
                    case 1:
                        frm.WB.GoForward();
                        break;
                    case 2:
                        frm.WB.Navigate(frm.WB.Url);
                        break;
                    case 3:
                        //frm.WB.GoHome();
                        break;
                    case 4:
                        frm.UpdateUserInfo();
                        break;
                }

                RunMode = -1;
            }
            catch(Exception err)
            {
                Console.WriteLine(err.Message);
            }
            base.OnMouseUp(e);
        }

        public override void Refresh()
        {
            InitBitmap();

            Graphics g = Graphics.FromImage(bitmap);
            Draw(g);

            Graphics gThis = CreateGraphics();

            gThis.DrawImage(bitmap, 1, 1);
            gThis.Dispose();

            g.Dispose();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            

            Refresh();
            base.OnMouseMove(e);
        }

        private void InitBitmap()
        {
            if (bitmap.Size.Equals(this.Size) == false)
            {
                bitmap.Dispose();
                bitmap = new Bitmap(Math.Max(1, Width), Math.Max(1, Height));
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if (RunMode != -1)
            {
                return;
            }

            Form frm = FindForm();

            if (frm != null)
            {
                if (frm.WindowState == FormWindowState.Maximized)
                {
                    frm.WindowState = FormWindowState.Normal;
                }
                else
                {
                    frm.WindowState = FormWindowState.Maximized;
                }

                frm.Focus();
            }
            base.OnDoubleClick(e);
        }

        BackgroundWorker Worker = new BackgroundWorker();

        private void TopPanel_Load(object sender, EventArgs e)
        {
            ResizeRedraw = true;
            Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            Worker.RunWorkerAsync(this);
        }

        private delegate void DrawHandle(TopPanel pnl);
        private static void ActionDraw(TopPanel pnl)
        {
            pnl.Refresh();
        }

        void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            TopPanel pnl = e.Argument as TopPanel;
            sw.Start();
            while (!CloseFlg)
            {
                if (sw.ElapsedMilliseconds > 100)
                {
                    sw.Reset();
                    sw.Start(); pnl.Invoke(new DrawHandle(ActionDraw), new object[] { e.Argument });
                   
                }

                System.Threading.Thread.Sleep(100);
            }
        }
    }
}

